﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

public partial class UserManage : System.Web.UI.Page
{
    static int managerID;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Label3.Text = "普通用户列表";
            string CmdStr = "select ID,UserName as 用户名,UserPwd as 密码 from tb_User";
            FillGridview(CmdStr);
        }
    }

    private SqlConnection CreateConn()
    {
        string connStr = ConfigurationManager.ConnectionStrings["ConStr"].ToString();
        SqlConnection conn = new SqlConnection(connStr);

        return conn;
    }

    private void FillGridview( string cmdStr)
    {
        SqlConnection conn = CreateConn();
        //string CmdStr = "select UserName as 用户名,UserPwd as 密码 from tb_User";
        try
        {
            SqlCommand comm = new SqlCommand(cmdStr, conn);
            conn.Open();
            SqlDataReader dr = comm.ExecuteReader();
            GridView1.DataSource = dr;
            GridView1.DataKeyNames = new string[] { "ID" };
            GridView1.DataBind();
            conn.Close();
        }
        catch (Exception ex)
        {
            conn.Close();
        }
    }

    protected string ChangeNum(string id)
    {
        string name;
        if (id == "1")
        {
            name = "管理员";
        }
        else
        {
            name = "普通用户";
        }

        return name;
    }

    protected void btn_User_Click(object sender, EventArgs e)//查看用户
    {
        Label3.Text = "普通用户列表";
        Panel1.Visible = true;
        Panel2.Visible = false;
        Panel3.Visible = false;

        btn_del.Visible = false;
        btn_add.Visible = false;
        //btn_Modify.Visible = false;
        string CmdStr = "select ID,UserName as 用户名,UserPwd as 密码 from tb_User";
        FillGridview(CmdStr);
    }
    protected void btn_manager_Click(object sender, EventArgs e)//查看管理员
    {
        Label3.Text = "管理员列表";
        Panel1.Visible = true;
        Panel2.Visible = false;
        Panel3.Visible = false;

        btn_del.Visible = true;
        btn_add.Visible = true;
        //btn_Modify.Visible = true;
        string CmdStr = "select ID,UserName as 用户名,UserPwd as 密码 from tb_User where flag=1";
        FillGridview(CmdStr);
    }
    protected void btn_del_Click(object sender, EventArgs e)
    {
        int n = 0;//标记显示提示对话框

        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            GridViewRow row = GridView1.Rows[i];

            bool isCheck = ((CheckBox)row.FindControl("CheckBox1")).Checked;
            int id = Convert.ToInt32(GridView1.DataKeys[row .DataItemIndex].Value);

            if (isCheck)
            {
                SqlConnection conn = CreateConn();
                try
                {
                    string cmdStr = "delete from tb_user where ID='" + id + "'";
                    SqlCommand comm = new SqlCommand(cmdStr, conn);
                    conn.Open();
                    comm.ExecuteNonQuery();
                    string CmdStr = "select ID,UserName as 用户名,UserPwd as 密码 from tb_User where flag=1";
                    FillGridview(CmdStr);
                }
                catch (Exception ex)
                {
                    conn.Close();
                }
            }
        }
    }
    protected void btn_Modify_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            GridViewRow row = GridView1.Rows[i];
            bool isCheck = ((CheckBox)row.FindControl("CheckBox1")).Checked;

            if (isCheck)
            {
                managerID = Convert.ToInt32(GridView1.DataKeys[row.DataItemIndex].Value);

                Panel1.Visible = false;
                Panel2.Visible = true;
                break;
            }
        }
    }
    protected void btn_OK_Click(object sender, EventArgs e)
    {
        if (txt_Pwd1.Text == "")
        {
            Label1.Text = "密码不允许为空";
            return;
        }

        if (txt_Pwd1.Text != txt_Pwd1.Text)
        {
            Label1.Text = "两次输入的密码不一致，请重新输入！";
            txt_Pwd1.Text = "";
            txt_Pwd2.Text = "";
            return;
        }

        if (txt_Pwd1.Text == txt_Pwd1.Text)
        {
            SqlConnection conn = CreateConn();

            try
            {
                string cmdStr = "update tb_User set UserPwd='" + txt_Pwd1.Text + "' where ID='" + managerID + "'";
                SqlCommand comm = new SqlCommand(cmdStr, conn);
                conn.Open();
                comm.ExecuteNonQuery();
                conn.Close();
                Label1.Text = "修改成功！";
            }
            catch (Exception ex)
            {
            }
        }
    }
    protected void btn_add_Click(object sender, EventArgs e)
    {
        Panel1.Visible = false;
        Panel2.Visible = false;
        Panel3.Visible = true;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (txt_User.Text == "")
        {
            Label2.Text = "用户名不允许为空！";
            txt_User.Text = "";
            return;
        }

        if (txt_Epwd2.Text == "")
        {
            Label2.Text = "密码不允许为空！";
            txt_Epwd1.Text = "";
            return;
        }

        if (txt_Epwd1.Text != txt_Epwd2.Text)
        {
            Label2.Text = "两次输入的密码不一致,重新输入！";
            return;
        }

        SqlConnection conn = CreateConn();

        try
        {
            string selcetStr = "select * from tb_User where UserName='" + txt_User.Text + "'";
            SqlCommand selectCmd = new SqlCommand(selcetStr, conn);
            //conn.Open();
            //SqlDataReader dr = selectCmd.ExecuteReader();  表里没有对象里有什么
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = selectCmd;

            DataSet ds = new DataSet();
            da.Fill(ds);

            if (ds.Tables[0].Rows.Count > 0)
            {
                Label2.Text = "该用户名已存在，请重新输入";
                txt_User.Text = "";
                return;
            }


            string CmdStr = "insert into tb_User(UserName,UserPwd,Flag,EID) values('" + txt_User.Text + "','" + txt_Epwd1.Text + "','1','0')";
            SqlCommand comm = new SqlCommand(CmdStr, conn);
            conn.Open();
            comm.ExecuteNonQuery();
            conn.Close();
            Label2.Text = "添加成功";
        }
        catch (Exception ex)
        {
        }

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Panel1.Visible = true;
        Panel2.Visible = false;
        Panel3.Visible = false;
    }
    protected void btn_Cancel_Click(object sender, EventArgs e)
    {
        Panel1.Visible = true;
        Panel2.Visible = false;
        Panel3.Visible = false;

    }
}